/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package com.lineage.server.datatables;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.lineage.L1DatabaseFactory;
import com.lineage.server.templates.L1FurnitureItem;
import com.lineage.server.utils.SQLUtil;

/**
 * Class <code>FurnitureItemTable</code> 数据库资料表:血盟小屋家具.
 * 
 * @author jrwz
 * @version 2012-7-11上午08:32:25
 * @see com.lineage.server.datatables
 * @since JDK1.7
 */
public final class FurnitureItemTable {
    /** 提示信息. */
    private static final Logger LOG = Logger.getLogger(FurnitureItemTable.class
            .getName());
    /** 实例. */
    private static FurnitureItemTable instance;

    /**
     * 取得实例(当第一次为空时才新建一个).
     * 
     * @return 实例
     */
    public static FurnitureItemTable getInstance() {
        if (instance == null) {
            instance = new FurnitureItemTable();
        }
        return instance;
    }

    /** 哈希集合:家具资料. */
    private final HashMap<Integer, L1FurnitureItem> furnishings = //
    new HashMap<Integer, L1FurnitureItem>();

    /** 数据库资料表:血盟小屋家具. */
    private FurnitureItemTable() {
        this.load();
    }

    /**
     * 取得家具:道具编号(哈希集合内).
     * 
     * @param itemId
     *            - 道具编号
     * @return 如果没有则返回null
     */
    public L1FurnitureItem getTemplate(final int itemId) {
        if (this.furnishings.containsKey(itemId)) {
            return this.furnishings.get(itemId);
        }
        return null;
    }

    /**
     * 加载.
     */
    private void load() {
        Connection con = null;
        PreparedStatement pstm = null;
        ResultSet rs = null;
        try {
            con = L1DatabaseFactory.getInstance().getConnection();
            pstm = con.prepareStatement("SELECT * FROM furniture_item");
            rs = pstm.executeQuery();
            while (rs.next()) {
                final L1FurnitureItem furniture = new L1FurnitureItem();
                final int itemId = rs.getInt("item_id");
                furniture.setFurnitureItemId(itemId);
                furniture.setFurnitureNpcId(rs.getInt("npc_id"));
                this.furnishings.put(Integer.valueOf(itemId), furniture);
            }
        } catch (final SQLException e) {
            LOG.log(Level.SEVERE, e.getLocalizedMessage(), e);
        } finally {
            SQLUtil.close(rs);
            SQLUtil.close(pstm);
            SQLUtil.close(con);
        }
    }
}
